package unibratec.projeto;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View.OnLongClickListener;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay {

	private ArrayList<OverlayItem> mol = new ArrayList<OverlayItem>();
	private Context ctx;
	GeoPoint p;
	private BitmapFactory img;
	
	public ItemizedOverlay(Drawable defaultMarker){
		super(boundCenterBottom(defaultMarker));
	}
	
	public ItemizedOverlay(Drawable defaultMarker, MapView mapa) {
		super(boundCenterBottom(defaultMarker));
		ctx = mapa.getContext();
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapa, boolean shadow, long when) {
		super.draw(canvas, mapa, shadow); 
		
        Point screenPts = new Point();
        mapa.getProjection().toPixels(p, screenPts);
        
        Bitmap img = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ponto);
		            
        canvas.drawBitmap(img, screenPts.x, screenPts.y-50, null);         
		return true;
		
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mol.get(i);
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mol.add(overlay);
	    populate();
	}
	
	public void removeOverLay(OverlayItem overlay){
		mol.remove(overlay);
		populate();
	}

	@Override
	public int size() {
		return mol.size();
	}
		
	protected boolean onTap(int index) {
		OverlayItem item = mol.get(index);
		
		item.getSnippet();
		item.getTitle();
		Log.d("Toast", item.getTitle() + " - " + item.getSnippet());
	
		
		Toast.makeText(ctx, mol.get(index).getTitle(), Toast.LENGTH_SHORT).show();
		return true;
	}
	
	public void addItem(GeoPoint p, String title, String snippet){
		OverlayItem newItem = new OverlayItem(p, title, snippet);
		mol.add(newItem);
		populate();
		}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapa) {
		if (event.getAction() == 1){
			GeoPoint p = mapa.getProjection().fromPixels(
					(int) event.getX(),
					(int) event.getY());
			Toast.makeText(ctx, p.getLatitudeE6() + " " + p.getLongitudeE6() , 
                    Toast.LENGTH_SHORT).show();
        }                
        return false;
	}
	
	
	
}
